package com.zzz.offer.replaceStr;

public class ReplaceStr {
    public static void main(String[] args) {
        System.out.println(new ReplaceStr().replaceSpace("2 333"));
    }

    public String replaceSpace(String s) {
        int spaceCount = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                spaceCount++;
            }
        }
        if (spaceCount > 0) {
            char[] s1 = new char[s.length() + 2 * spaceCount];
            int j = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == ' ') {
                    s1[j++] = '%';
                    s1[j++] = '2';
                    s1[j++] = '0';
                } else {
                    s1[j++] = s.charAt(i);
                }
            }
            return new String(s1);
        }

        return s;
        // write code here
    }

}
